<?php
/*
Template Name: comparison Result
Auther: Naseer Ahnad
Created: 10-03-2012
*/
?>
<?php 
global $wpdb;


function get_category_id($cat_name){
	$term = get_term_by('name', $cat_name, 'category');
	return $term->term_id;
}

function get_post_meta_all($post_id){
    global $wpdb;
    $data   =   array();
    $wpdb->query("
        SELECT `meta_key`, `meta_value`
        FROM $wpdb->postmeta
        WHERE `post_id` = $post_id
    ");
    foreach($wpdb->last_result as $k => $v){
        $data[$v->meta_key] =   $v->meta_value;
    };
    return $data;
}

function isYes($val){
	$val=strtolower(trim($val));
	if($val=="yes" or $val=="y") return true;
	return false;
}

function getVenuInfo($query, $venueID){
		
//	echo $venueID;
	$data=array();
	$vInfo= get_post_meta_all($venueID);
	
//	print "<pre>";
//	print_r($vInfo);
//	print "</pre>";


	///////// Day Delegate
	if(isYes($vInfo['dayDelegate'])){
		if(isInRange($vInfo['dayDelegateRange'], $query['dayDelegate'])==true){
			$data['dayDelegate']=$query['dayDelegate'] * $vInfo['dayDelegateRate'];
		}
		else
			$data['dayDelegate']=0;	
	}
	else
		$data['dayDelegate']=0;


	///////// 24Hr Delegate
	if(isYes($vInfo['24HrDelegate'])){
		if(isInRange($vInfo['24HrDelegateRange'], $query['24HrDelegateGuests'])==true){
			$data['24HrDelegate']=$query['24HrDelegateGuests'] * $vInfo['24HrDelegateRate'];
		}
		else
			$data['24HrDelegate']=0;	
	}
	else
		$data['24HrDelegate']=0;

	///////// Outside Area
	if(isYes($vInfo['outsideArea']) && $query['outsideArea']=='Yes'){
		//if(isInRange($vInfo['outsideAreaRange'], $query['guestsBasic'])==true){
			$data['outsideArea']=$vInfo['outsideAreaRate'];
		//}
		//else
		//	$data['outsideArea']=0;	
	}
	else
		$data['outsideArea']=0;


	///////// Room Single
	if(isYes($vInfo['roomSingle'])){
		if(isInRange($vInfo['roomSingleRange'], $query['roomSingle'])==true){
			$data['roomSingle']=$query['roomSingle'] * $vInfo['roomSingleRate'];
		}
		else
			$data['roomSingle']=0;	
	}
	else
		$data['roomSingle']=0;
	
	///////// Room Double
	if(isYes($vInfo['roomDouble'])){
		if(isInRange($vInfo['roomDoubleRange'], $query['roomDouble'])==true)
			$data['roomDouble']=$query['roomDouble'] * $vInfo['roomDoubleRate'];
		else
			$data['roomDouble']=0;	
	}
	else
		$data['roomDouble']=0;


	///////// Bed & Breakfast Single
	if(isYes($vInfo['breakfastSingle'])){
		if(isInRange($vInfo['breakfastSingleRange'], $query['breakfastSingle'])==true){
			$data['breakfastSingle']=$query['breakfastSingle'] * $vInfo['breakfastSingleRate'];
		}
		else
			$data['breakfastSingle']=0;	
	}
	else
		$data['breakfastSingle']=0;
	
	///////// Bed & Breakfast Double
	if(isYes($vInfo['breakfastDouble'])){
		if(isInRange($vInfo['breakfastDoubleRange'], $query['breakfastDouble'])==true)
			$data['breakfastDouble']=$query['breakfastDouble'] * $vInfo['breakfastDoubleRate'];
		else
			$data['breakfastDouble']=0;	
	}
	else
		$data['breakfastDouble']=0;

	///////// Dinner Bed & Breakfast Single
	if(isYes($vInfo['dinnerSingle'])){
		if(isInRange($vInfo['dinnerSingleRange'], $query['dinnerSingle'])==true){
			$data['dinnerSingle']=$query['dinnerSingle'] * $vInfo['dinnerSingleRate'];
		}
		else
			$data['dinnerSingle']=0;	
	}
	else
		$data['dinnerSingle']=0;
	
	///////// Dinner Bed & Breakfast Double
	if(isYes($vInfo['dinnerDouble'])){
		if(isInRange($vInfo['dinnerDoubleRange'], $query['dinnerDouble'])==true)
			$data['dinnerDouble']=$query['dinnerDouble'] * $vInfo['dinnerDoubleRate'];
		else
			$data['dinnerDouble']=0;	
	}
	else
		$data['dinnerDouble']=0;


	///////// flipCharts
	if(isYes($vInfo['flipCharts'])){
		if(isInRange($vInfo['flipChartsRange'], $query['flipCharts'])==true){
			$data['flipCharts']=$query['flipCharts'] * $vInfo['flipChartsRate'];
		}
		else
			$data['flipCharts']=0;	
	}
	else
		$data['flipCharts']=0;

	///////// lcdHire
	if(isYes($vInfo['lcdHire'])){
		if(isInRange($vInfo['lcdHireRange'], $query['lcdHire'])==true){
			$data['lcdHire']=$query['lcdHire'] * $vInfo['lcdHireRate'];
		}
		else
			$data['lcdHire']=0;	
	}
	else
		$data['lcdHire']=0;

	///////// wifi
	if(isYes($vInfo['wifi']) && $query['wifi']=='Yes'){
		//if(isInRange($vInfo['wifiRange'], $query['guestsAdditional'])==true){
			$data['wifi']=$vInfo['wifiRate'];
		//}
		//else
		//	$data['wifi']=0;	
	}
	else
		$data['wifi']=0;

	///////// conferenceCall
	if(isYes($vInfo['conferenceCall']) && $query['conferenceCall']=='Yes'){
		//if(isInRange($vInfo['conferenceCallRange'], $query['guestsAdditional'])==true){
			$data['conferenceCall']=$vInfo['conferenceCallRate'];
		//}
		//else
		//	$data['conferenceCall']=0;	
	}
	else
		$data['conferenceCall']=0;

	///////// teaCoffee
	if(isYes($vInfo['teaCoffee'])){
		if(isInRange($vInfo['teaCoffeeRange'], $query['teaCoffee'])==true){
			$data['teaCoffee']=$query['teaCoffee'] * $vInfo['teaCoffeeRate'];
		}
		else
			$data['teaCoffee']=0;	
	}
	else
		$data['teaCoffee']=0;

	///////// bottledWater
	if(isYes($vInfo['bottledWater'])){
		if(isInRange($vInfo['bottledWaterRange'], $query['bottledWater'])==true){
			$data['bottledWater']=$query['bottledWater'] * $vInfo['bottledWaterRate'];
		}
		else
			$data['bottledWater']=0;	
	}
	else
		$data['bottledWater']=0;

	///////// courceMeal2
	if(isYes($vInfo['courceMeal2'])){
		if(isInRange($vInfo['courceMeal2Range'], $query['courceMeal2'])==true){
			$data['courceMeal2']=$query['courceMeal2'] * $vInfo['courceMeal2Rate'];
		}
		else
			$data['courceMeal2']=0;	
	}
	else
		$data['courceMeal2']=0;

	///////// courseMeal3
	if(isYes($vInfo['courseMeal3'])){
		if(isInRange($vInfo['courseMeal3Range'], $query['courseMeal3'])==true){
			$data['courseMeal3']=$query['courseMeal3'] * $vInfo['courseMeal3Rate'];
		}
		else
			$data['courseMeal3']=0;	
	}
	else
		$data['courseMeal3']=0;

	///////// hotBuffet
	if(isYes($vInfo['hotBuffet'])){
		if(isInRange($vInfo['hotBuffetRange'], $query['hotBuffet'])==true){
			$data['hotBuffet']=$query['hotBuffet'] * $vInfo['hotBuffetRate'];
		}
		else
			$data['hotBuffet']=0;	
	}
	else
		$data['hotBuffet']=0;

	///////// coldBuffet
	if(isYes($vInfo['coldBuffet'])){
		if(isInRange($vInfo['coldBuffetRange'], $query['coldBuffet'])==true){
			$data['coldBuffet']=$query['coldBuffet'] * $vInfo['coldBuffetRate'];
		}
		else
			$data['coldBuffet']=0;	
	}
	else
		$data['coldBuffet']=0;
	
	///// discount and totals
	//$total=array_sum($data);
	//$data['discount']=intval($vInfo['discount']);
	//$data['total']=$total;
	//$data['gtotal']=$total-($total*$data['discount']/100);
	$data['gtotal']=array_sum($data);
	
	$data['rating']=$vInfo['rating'];
	return $data;
}

function isInRange($range, $value){
	$range = trim($range);
	$value=floatval(trim($value));
	
	if($range=="") return true;
	
	if(substr($range,-1,1)=="+"){
		$digit=number_format($range);
		if($value<=$digit) return true; else return false;
	}
	
	$digits = explode("-", $range);
	$digits[0]=intval($digits[0]);
	if(count($digits)>1) $digits[1]=intval($digits[1]);
	
	if(count($digits)==1)
		if($value<=$digits[1]) return true; else return false;
	else
		if($value>=$digits[0] and $value<=$digits[1]) return true; else return false;
	
	return false;
}

/////// get all parents of specifc area
$parentCatList = get_category_parents($_POST['area'],false,',');
if(is_string($parentCatList)) $parentCatListArray = split(",",$parentCatList); else $parentCatListArray=array();
if(count($parentCatListArray)>0) array_shift($parentCatListArray);		///////// removing top most category i-e Countries

$areaID=array();
foreach($parentCatListArray as $pCat){
	if(trim($pCat)!="") $areaID[]=get_category_id(trim($pCat));
}

$typeID = array(8, $_POST['venue']);

query_posts(array('category__and'=> $typeID, 'category__in'=> $areaID));
$venueIDs = array();

if(have_posts()){
	while ( have_posts() ) : the_post();
		$venueIDs[]=array('ID'=>get_the_ID(), "Name"=>get_the_title());
	endwhile;	
}

///////////// Geting Data
$venuesRates=array();

foreach($venueIDs as $venue){
	$venuesRates[]=getVenuInfo($_POST, $venue['ID']);
}

//ob_end_clean();

//print "<pre>";
//print_r($venuesRates);
//print "</pre>";
//exit;

get_header(); 
?>
<script type="text/javascript">
function updateRating(id, value){
	postID = id.substr(5, id.length-5);
	jQuery.post("http://armchairweddingguru.co.uk/comparison-result/",{postID:postID, value:value, action:"updateRate"}, function(data){
		alert(data);
	});
}
</script>
<div class="main-content-inner global-width">
    
    <section class="contents full-width">
        <h1>Wedding Venue Comparision</h1>

        <table>
        <tr>
            <th width="220"></th>
            <th width="70">Item</th>
            <th width="50">Pr @</th>
            <?php foreach($venueIDs as $venue): ?>
            <th width="100"><a href="<?php echo  get_permalink( $venue['ID'] ); ?>"><?=$venue['Name']?></a></th>
            <?php endforeach; ?>
        </tr>

<!--        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>
            	<?php $i=0; foreach($venueIDs as $venue): ?>
            	<select onchange="updateRating(this.id, this.value)" id="rate-<?php echo $venue['ID'] ?>">
                <option value="5" <?php if(trim($venuesRates[$i]['rating'])=="5") echo 'selected="selected"' ?>>Excellent</option>
                <option value="4" <?php if(trim($venuesRates[$i]['rating'])=="4") echo 'selected="selected"' ?>>Good</option>
                <option value="3" <?php if(trim($venuesRates[$i]['rating'])=="3") echo 'selected="selected"' ?>>Average</option>
                <option value="2" <?php if(trim($venuesRates[$i]['rating'])=="2") echo 'selected="selected"' ?>>Below Average</option>
                <option value="1" <?php if(trim($venuesRates[$i]['rating'])=="1") echo 'selected="selected"' ?>>Poor</option>
                </select>
                <?php $i++; endforeach; ?>
            </td>
        </tr>
-->
        
        <tr>
            <td><h2 align="center">Room Options</h2></td>
        </tr>
        
        <tr>
            <th>Day Delegate Rate:</th>
            <th><?=($_POST['dayDelegate']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['dayDelegate']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['dayDelegate'],2)?></td>
            <?php endforeach; ?>
        </tr>
        
        <tr>
            <th>24Hr Delegate Rate:</th>
            <th><?=($_POST['24HrDelegate']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['24HrDelegate']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['24HrDelegate'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <th>Outside Area:</th>
            <th><?=$_POST['outsideArea']?></th>
            <th>&nbsp;</th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['outsideArea'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <td><h2 align="center">Accommodation Charges</h2></td>
        </tr>
        
        <tr>
            <th>Room Only (Single):</th>
            <th><?=($_POST['roomSingle']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['roomSingle']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['roomSingle'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
            <th>Room Only (Double/Twin):</th>
            <th><?=($_POST['roomDouble']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['roomDouble']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['roomDouble'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
            <th>Bed & Breakfast (Single):</th>
            <th><?=($_POST['breakfastSingle']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['breakfastSingle']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['breakfastSingle'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
            <th>Bed & Breakfast (Double/Twin):</th>
            <th><?=($_POST['breakfastDouble']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['breakfastDouble']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['breakfastDouble'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <th>Dinner Bed & Breakfast (Single):</th>
            <th><?=($_POST['dinnerSingle']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['dinnerSingle']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['dinnerSingle'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
            <th>Dinner Bed & Breakfast (Double/Twin):</th>
            <th><?=($_POST['dinnerDouble']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['dinnerDouble']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['dinnerDouble'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        
        <tr>
            <td><h2 align="center">Additional Requirements/Charges</h2></td>
        </tr>
        
        <tr>
          <th>Flip Charts:</th>
            <th><?=($_POST['flipCharts']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['flipCharts']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['flipCharts'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
          <th>LCD Hire:</th>
            <th><?=($_POST['lcdHire']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['lcdHire']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['lcdHire'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
          <th>WiFi:</th>
            <th><?=$_POST['wifi']?></th>
            <th>&nbsp;</th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['wifi'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
          <th>Conference Call Facilities:</th>
            <th><?=$_POST['conferenceCall']?></th>
            <th>&nbsp;</th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['conferenceCall'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
          <th>Tea / Coffee:</th>
            <th><?=($_POST['teaCoffee']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['teaCoffee']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['teaCoffee'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
          <th>Bottled Water:</th>
            <th><?=($_POST['bottledWater']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['bottledWater']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['bottledWater'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
          <th>2 Course Meal:</th>
            <th><?=($_POST['courceMeal2']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['courceMeal2']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['courceMeal2'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
          <th>3 Course Meal:</th>
            <th><?=($_POST['courseMeal3']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['courseMeal3']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['courseMeal3'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
          <th>Hot Buffet:</th>
            <th><?=($_POST['hotBuffet']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['hotBuffet']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['hotBuffet'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        <tr>
          <th>Cold Buffet:</th>
            <th><?=($_POST['coldBuffet']=="0"?"No":"Yes")?></th>
            <th><?=$_POST['coldBuffet']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['coldBuffet'],2)?></td>
            <?php endforeach; ?>
            
        </tr>

        
        <tr>
            <td><h2 align="center">Total</h2></td>
        </tr>
        
        <tr>
            <td colspan="3"><strong>Gross Total</strong></td>
            <?php foreach($venuesRates as $venuesRate): ?>
            <td><strong>&pound;<?=number_format($venuesRate['gtotal'],2)?></strong></td>
            <?php endforeach; ?>
            
        </tr>
        
        </table>

        <div class="clear"></div>
    </section>
    <div class="clearfix"></div>
  </div>

<!-- Main Content Ends -->
</section>
<?php get_footer(); ?>